from PySide6 import QtWidgets
from PySide6.QtWidgets import QMessageBox, QInputDialog
from PySide6.QtCore import Slot

from util.exercise import Exercise
from util.equation import Equation
import util.exercise_file_dao as dao


class ArithmeticCreator(QtWidgets.QWidget):
    exercise: Exercise = Exercise()

    def __init__(self):
        super().__init__()
        self.title = "生成习题"

        self.setWindowTitle(self.title)
        self.setFixedSize(400, 220)

        # self.lb_title = QtWidgets.QLabel(
        #     self.title, alignment=QtCore.Qt.AlignCenter)

        self.lb_range = QtWidgets.QLabel("范围（默认0~100）")
        self.lb_min = QtWidgets.QLabel("最小值：")
        self.lb_max = QtWidgets.QLabel("最大值：")
        self.ld_min = QtWidgets.QLineEdit()
        self.ld_max = QtWidgets.QLineEdit()

        self.lb_add = QtWidgets.QLabel("加法个数：")
        self.lb_sub = QtWidgets.QLabel("减法个数：")
        self.ld_add = QtWidgets.QLineEdit()
        self.ld_sub = QtWidgets.QLineEdit()

        self.btn_add = QtWidgets.QPushButton("加法算式")
        self.btn_sub = QtWidgets.QPushButton("减法算式")
        self.btn_mix = QtWidgets.QPushButton("混合算式")
        self.btn_csv = QtWidgets.QPushButton("输出 CSV")
        self.btn_online = QtWidgets.QPushButton("在线答题")

        # 没有习题时禁用按钮
        self.btn_csv.setDisabled(True)
        self.btn_online.setDisabled(True)

        self.btn_home = QtWidgets.QPushButton("返回首页")

        # 绑定事件
        self.btn_add.clicked.connect(self.handle_add)
        self.btn_sub.clicked.connect(self.handle_sub)
        self.btn_mix.clicked.connect(self.handle_mix)
        self.btn_csv.clicked.connect(self.handle_csv)

        # 布局
        grid = QtWidgets.QGridLayout()

        grid.addWidget(self.lb_range, 0, 0, 1, 2)
        grid.addWidget(self.lb_min, 1, 0)
        grid.addWidget(self.lb_max, 1, 1)
        grid.addWidget(self.ld_min, 2, 0)
        grid.addWidget(self.ld_max, 2, 1)

        grid.addWidget(self.lb_add, 3, 0)
        grid.addWidget(self.lb_sub, 3, 1)
        grid.addWidget(self.ld_add, 4, 0)
        grid.addWidget(self.ld_sub, 4, 1)

        vbox = QtWidgets.QVBoxLayout(self)
        hbox1 = QtWidgets.QHBoxLayout()
        hbox2 = QtWidgets.QHBoxLayout()

        hbox1.addWidget(self.btn_add)
        hbox1.addWidget(self.btn_sub)
        hbox1.addWidget(self.btn_mix)

        hbox2.addWidget(self.btn_csv)
        hbox2.addWidget(self.btn_online)

        vbox.addLayout(grid)
        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)
        vbox.addWidget(self.btn_home)

    @Slot()
    def handle_csv(self):
        text, ok = QInputDialog.getText(self, "提示", "输入文件名：")
        if not ok:
            print("用户取消输入文件名")
            return
        num, ok = QInputDialog.getInt(self, "提示", "输入每行算式的个数：", value=5)
        if not ok:
            print("用户取消输入算式个数")
            return

        num = num if num else 5

        filename = f"{text}.csv"
        dao.write_exercise_to_file(filename, num, self.exercise)

        # 保证不重复导出习题
        self.exercise.equationList.clear()
        self.btn_csv.setDisabled(True)
        self.btn_online.setDisabled(True)

    @Slot()
    def handle_add(self):
        try:
            d_min = self.ld_min.text()
            d_max = self.ld_max.text()
            d_add = int(self.ld_add.text())
            # d_sub = int(self.ld_sub.text())

            d_min = d_min if d_min else 0
            d_max = d_max if d_max else 100
            d_min = int(d_min)
            d_max = int(d_max)
            d_sub = 0
            # print(d_min, d_max, d_add, d_sub)

            self.creator(d_min, d_max, d_add, d_sub)

        except:
            QMessageBox.warning(self, "参数错误", "输入有误")

    @Slot()
    def handle_sub(self):
        try:
            d_min = self.ld_min.text()
            d_max = self.ld_max.text()
            # d_add = int(self.ld_add.text())
            d_sub = int(self.ld_sub.text())

            d_min = d_min if d_min else 0
            d_max = d_max if d_max else 100
            d_min = int(d_min)
            d_max = int(d_max)
            d_add = 0
            # print(d_min, d_max, d_add, d_sub)

            self.creator(d_min, d_max, d_add, d_sub)

        except:
            QMessageBox.warning(self, "参数错误", "输入有误")

    @Slot()
    def handle_mix(self):
        try:
            d_min = self.ld_min.text()
            d_max = self.ld_max.text()
            d_add = int(self.ld_add.text())
            d_sub = int(self.ld_sub.text())

            d_min = d_min if d_min else 0
            d_max = d_max if d_max else 100
            d_min = int(d_min)
            d_max = int(d_max)
            # print(d_min, d_max, d_add, d_sub)

            self.creator(d_min, d_max, d_add, d_sub)

        except:
            QMessageBox.warning(self, "参数错误", "输入有误")

    def creator(self, d_min, d_max, d_add, d_sub):
        # 设置 Equation 范围区间
        Equation.rangeMin = d_min
        Equation.rangeMax = d_max

        # 生成习题
        self.exercise.generateExercise(d_add, d_sub)
        if len(self.exercise.equationList) > 0:
            self.btn_csv.setDisabled(False)
            self.btn_online.setDisabled(False)
        else:
            self.btn_csv.setDisabled(True)
            self.btn_online.setDisabled(True)
